package com.taihe.util;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * Created by zwt on 2018/7/23.
 */
public class RedisUtil {

    private static RedisTemplate<String, Object> redisTemplate;

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public static boolean setExpire(String key,Object value,long val) {
        try {
            redisTemplate.opsForValue().set(key, value,val);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean set(String key,Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Object get(String key){
        ValueOperations<String, Object> os = redisTemplate.opsForValue();
        if(null==key){
          return null;
        }
        Object object = os.get(key);
        if(null==object){
            return null;
        }else{
            return object;
        }
    }

    public static boolean put(String key,String objectKey,Object val){
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        try {
            ops.put(key,objectKey,val);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Object getObj(String objectKey,String key){
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        if(null==key || null==objectKey){
            return null;
        }
        Object object = ops.get(key, objectKey);
        if(null==object){
            return null;
        }else{
            return object;
        }
    }

}
